//
//  MyCollectionViewController.m
//  WuLi
//
//  Created by Gollum on 16/9/3.
//  Copyright © 2016年 Gollum. All rights reserved.
//

#import "MyCollectionViewController.h"

#import "BigImageCellTableViewCell.h"
#import "PettyTableViewCell.h"
#import "MJRefresh.h"
#import "NetworkTool.h"
#import "UIImageView+WebCache.h"

#import "CollectionListData.h"
#import "CollectionListModel.h"
#import "CollectionListNewscoverinfo.h"
#import "CollectionListNewsimageinfolist.h"
#import "CollectionListNewssummaryinfolist.h"
#import "CollectionListParamap.h"


#import "BIgImageViewController.h"
#import "PettyImageViewController.h"


#define kCollectionPettyTableViewCellReuse @"CollectionPettyTableViewCellReuse"
#define kCollectionBigImageCellTableViewCellReuse @"CollectionBigImageCellTableViewCellReuse"

@interface MyCollectionViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong)CollectionListModel *collectionModel;
@property (nonatomic,strong)NSMutableArray *dataArray;

@end

@implementation MyCollectionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _dataArray = [NSMutableArray array];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self handleData];
    [_tableView registerNib:[UINib nibWithNibName:@"BigImageCellTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kCollectionBigImageCellTableViewCellReuse];
     [_tableView registerNib:[UINib nibWithNibName:@"PettyTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kCollectionPettyTableViewCellReuse];
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self handleData];
            
            [_tableView.mj_header endRefreshing];
            
        });
    }];
    
    
    [self.tableView.mj_header beginRefreshing];

}
- (void)handleData{

  NSString *urlString = @"http://211.151.51.100/v1/news/collect/list?";
    NSDictionary *header = @{@"User-Agent":@"Dalvik/2.1.0 (Linux; U; Android 5.0; ASUS_Z00ADB Build/LRX21V) Wuli/2.4.0 (agent:s;channel:pphl;credential:e1MyMDIwMDAxMDF9LXsjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIzM1Nzg3MjA2MTcxNDM4M30teyMjIyMjIyMxNDcyMDA5NzE2NTcwLTU0OTIyZjhiZTV9QDE0NzQwNzgyODczNjU=;deviceId:357872061714383;osTypeId:01;detailInfo:android;simTypeId:01;netTypeId:01;deviceTypeId:01;osVersion:5.0;idfa:null)", @"Host":@"api.5wuli.com"};
    [NetworkTool GET:urlString andBody:nil andHeader:header andResponse:JHJSON andSuccessBlock:^(id result) {
        
        NSLog(@"_________________________我%@",result);
        _collectionModel = [CollectionListModel modelWithDic:result];
        [_dataArray addObject:_collectionModel];
      
        [self.tableView reloadData];
        
    } andFailureBlock:^(NSError *error) {
        
    }];

    


}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([_collectionModel.data[indexPath.row].layoutType isEqualToString:@"BIG"]) {
        return 340;
    }else{
    
        return 115;
    
    }


}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSLog(@"~~~~~~%ld",_collectionModel.data.count);
    return _collectionModel.data.count;

}




- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
//    
//    [tableView cellForRowAtIndexPath:indexPath];
//    if (cell == nil) {
//        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"pool1"];
//    }
//    return cell;
    if ([_collectionModel.data[indexPath.row].layoutType isEqualToString:@"BIG"]) {
        
        BigImageCellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCollectionBigImageCellTableViewCellReuse];
      
        NSURL *url = [NSURL URLWithString:_collectionModel.data[indexPath.row].newsSummaryInfoList[0].newsImageInfoList[0].url];
        [cell.mainImage sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"default_image"]];
        cell.title.text = _collectionModel.data[indexPath.row].newsSummaryInfoList[0].title;
        cell.origin.text = _collectionModel.data[indexPath.row].newsSummaryInfoList[0].origin;
      
        return cell;
    }else{
        PettyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCollectionPettyTableViewCellReuse];
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:_collectionModel.data[indexPath.row].newsSummaryInfoList[0].newsImageInfoList[0].url]];
        cell.mainImage.image = [UIImage imageWithData:data];
        cell.title.text = _collectionModel.data[indexPath.row].newsSummaryInfoList[0].title;
        
        cell.origin.text = _collectionModel.data[indexPath.row].newsSummaryInfoList[0].origin;
        
//        cell.commentTotalCount.text = [NSString stringWithFormat:@"%ld",_recModel.data[indexPath.row].commentTotalCount];
        
        return cell;

    }

   
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    if ([_collectionModel.data[indexPath.row].layoutType isEqualToString:@"BIG"]) {
        BIgImageViewController *big = [[BIgImageViewController alloc] init];
        big.detailString = _collectionModel.data[indexPath.row].newsSummaryInfoList[0].detailLink;
        [self presentViewController:big animated:YES completion:^{
        }];
        
    }else{
    
        PettyImageViewController *petty = [[PettyImageViewController alloc] init];
        petty.detailString = _collectionModel.data[indexPath.row].newsSummaryInfoList[0].detailLink;
        [self presentViewController:petty animated:YES completion:^{
            
        }];
    
    
    }


}




- (IBAction)handleBack:(id)sender {
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
- (IBAction)editButton:(id)sender {
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
